#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>

//#define FILE_MANAGEABLE_DEVICE "/tmp/manageable_device.txt"
#define FILE_MANAGEABLE_DEVICE "./manageable_device.txt"
#define MAX_TBL_NUM 64

typedef struct mngdev_s{
	char oui[64];
	char serial[64];
	char class[64];
	char mac[64];
}mngdev_t;

typedef struct mngtbl_s{
	mngdev_t arr[MAX_TBL_NUM];
	int num;
}mngtbl_t;

void tbl_load(mngtbl_t *tbl)
{
	FILE *fp = NULL;
	int i = 0;
	int ret = 0;
	char line[512] = {0};

	fp = fopen(FILE_MANAGEABLE_DEVICE, "r");
	if(!fp)
		return;

	while(!feof(fp))
	{
		if(i >= MAX_TBL_NUM)
			break;

		memset(line, 0, sizeof(line));
		fgets(line, sizeof(line), fp);
		ret = sscanf(line,"oui=%[^,], serial=%[^,], class=%[^,], mac=%[^\n]",
			tbl->arr[i].oui, tbl->arr[i].serial, tbl->arr[i].class, tbl->arr[i].mac);
		if(ret == 4)
		{
			i++;
		}
	}
	tbl->num = i;
	fclose(fp);
}

void tbl_update(mngtbl_t *tbl, mngdev_t *dev)
{
	int i = 0;
	int found = 0;

	printf("tbl update oui=[%s], serial=[%s], class=[%s], mac=[%s]\n", 
		dev->oui, dev->serial, dev->class, dev->mac);
	for(i = 0; i < tbl->num; i++)
	{
		if(0 == strcmp(tbl->arr[i].mac, dev->mac))
		{
			found = 1;
			memcpy(&(tbl->arr[i]), dev, sizeof(mngdev_t));
			break;
		}
	}

	if(!found && (tbl->num < MAX_TBL_NUM))
	{
		memcpy(&(tbl->arr[tbl->num]), dev, sizeof(mngdev_t));
		tbl->num++;
	}
}

void tbl_save(mngtbl_t *tbl)
{
	FILE *fp = NULL;
	int i = 0;
	char line[512] = {0};

	fp = fopen(FILE_MANAGEABLE_DEVICE, "w+");
	if(!fp)
		return;

	for(i = 0; i < tbl->num; i++)
	{
		fprintf(fp, "oui=%s, serial=%s, class=%s, mac=%s\n", 
			tbl->arr[i].oui, tbl->arr[i].serial, tbl->arr[i].class, tbl->arr[i].mac);
	}

	fclose(fp);	
}

void tbl_print(mngtbl_t *tbl)
{
	int i = 0;

	printf("----- tbl print -----\n");
	for(i = 0; i < tbl->num; i++)
	{
		printf("tbl[%d] oui=[%s], serial=[%s], class=[%s], mac=[%s]\n",
			i, tbl->arr[i].oui, tbl->arr[i].serial, tbl->arr[i].class, tbl->arr[i].mac);
	}
}

#if 0
void save_option_125()
{
  FILE *fp = NULL;
  char client_mac[64] = {0};
  char newoui[64] = {0};
  char newserial[64] = {0};
  char newclass[64] = {0};
  int count = 0, slen = 0, olen = 0, clen = 0;

  if (oui != NULL && serial != NULL && class != NULL)
  {
    print_mac(client_mac, emac, emac_len);
    fp = fopen(FILE_MANAGEABLE_DEVICE, "a");
    if (fp != NULL)
    {
      olen = oui[1];
      slen = serial[1];
      clen = class[1];
      memcpy(newoui, oui+2, olen);
      newoui[olen] = '\0';
      memcpy(newserial, serial+2, slen);
      newserial[slen] = '\0';
      memcpy(newclass, class+2, clen);
      newclass[clen] = '\0';

      fprintf(fp, "oui=%s, serial=%s, class=%s, mac=%s\n", newoui, newserial, newclass, client_mac);
      fclose(fp);
    }
  }
}
#endif

void main()
{
	mngtbl_t tbl;
	mngdev_t dev = {"aaaa", "bbbb", "cccc", "dddd"};
	mngdev_t dev2 = {"AAAAAA", "12345678", "router", "08:00:27:80:45:ed"};

	tbl_load(&tbl);
	tbl_print(&tbl);
	tbl_update(&tbl, &dev);
	tbl_save(&tbl);
	tbl_print(&tbl);
	tbl_update(&tbl, &dev2);
	tbl_save(&tbl);
	tbl_print(&tbl);
}
